#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

#include <QtGui>
#include <QtWebKit>
#include "qgeopositioninfosource.h"




const QString GMAPS_STATICMAP_URL_TEMPLATE =  "http://maps.google.com/staticmap?center=%1,%2&zoom=14&size=%3x%4&map type=mobile&markers=%1,%2&key=ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA&sensor=false";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
   // QGeoPositionInfoSource *source = new QGeoPositionInfoSource(this);
    if(source)
    {
    //source->preferredPositioningMethods(AllPositioningMethods);
    //source->setUpdateInterval(1500);
    connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
                this, SLOT(positionUpdated(QGeoPositionInfo)));
    source->requestUpdate(15000);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::positionUpdated(const QGeoPositionInfo &info)
{

ui->label->setText(tr("Position: %1").arg(info.coordinate().toString()));

   // QMessageBox::information(this, tr("Info"),info);
int width = qMin(ui->webView->width(), 600);
int height = qMin(ui->webView->height(), 360);
        /* google maps url.
        QString url_gmap = GMAPS_STATICMAP_URL_TEMPLATE
                            .arg(QString::number(info.coordinate().latitude()))
                            .arg(QString::number(info.coordinate().longitude()))
                            .arg(QString::number(width))
                            .arg(QString::number(height));
            //.arg("&path=color:0xff0000ff|weight:5|40.737102,-73.990318|40.749825,-73.987963|40.752946,-73.987384|40.755823,-73.986397");
        */
        //note: example url http://m.ovi.me/?hc&h=200&w=200&?c=52.531,13.3845&z=17
        QString url_ovi;
        url_ovi.sprintf("http://m.ovi.me/?hc&h=%d&w=%d&?c=%f,%f&z=17",width,height,
                        info.coordinate().latitude(),info.coordinate().longitude());
       ui->webView->load(url_ovi);// pass in url_gmap if want to use google map
}

